<template>
	<view>
		
		<view style="padding: 40rpx;background-color: #fff;">
			<u-form :model="cardForm" ref="uForm" label-position="top">
				<u-form-item prop="card_no" label="卡号" required><u-input v-model="cardForm.card_no" placeholder="请输入提货卡号" /></u-form-item>
				<u-form-item prop="secret" label="密码" required><u-input v-model="cardForm.secret" placeholder="请输入提货卡密码" /></u-form-item>
				<u-form-item></u-form-item>
			</u-form>
			<u-button type="warning" @click="submit">兑换</u-button>
		</view>
		
		
	</view>
</template>

<script>
	export default{
		data(){
			return {
				cardForm:{
					card_no:'',
					secret:''
				},
				rules: {
					card_no: [
						{ 
							required: true, 
							message: '请输入提货卡号', 
							trigger: ['change','blur'],
						}
					],
					secret: [
						{
							required: true,
							message: '请输入提货卡密码', 
							trigger: ['change','blur'],
						}
					]
				},
				
			}
		},
		methods:{
			submit(){
				let self = this;
				self.$refs.uForm.validate(async valid => {
					if (valid) {
						let res = await self.$http.request({url:self.$api.take_product.verify,data:self.cardForm,showLoading:true});
						if(res.code !== 0){
							return self.$http.toast(res.msg);
						}
						uni.navigateTo({url:'/plugins/take_product/exchange?card_no='+self.cardForm.card_no+"&secret="+self.cardForm.secret});
					}
					
				});
			},
			chooseAddr() { //选择地址
				uni.navigateTo({
					url: "/pages/user/address/list?name=picker_address"
				})
			},
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		async onLoad(opt){
			this.cardForm.card_no = opt.card_no ?? '';

			//未登录的情况下，需要绑定提货卡归属人为上级。这里查询提货卡归属人信息，然后设置缓存
			if ( !uni.getStorageSync('token') ) {
				let res = await this.$http.request({url:this.$api.take_product.cardInfo,data:this.cardForm,showLoading:true});
				if(res.code === 0){
					return uni.setStorageSync('pid',res.data.user_id);
				}
			}
		}
	}
</script>

<style lang="scss">

</style>